
clear
set more off
capture log close
set seed 12345	
set matsize 10000
set maxvar 20000
global OUTPUT "$DIR/Output"
global DATA "$DIR"	
cd "$DIR"

/* SET LOCALS */
* set earliest tax year to keep
local yrb = 2008
* set last tax year to keep
local yre = 2016
* set excluded base year for event-time and controls
local baseyr "2012"
* set tc sample (ncg nbus)
local ivsamp "ncg"
* average or not ("_avg" or "")
local ivavg ""


use $DATA/reg_file_firm, clear

************************************************************************************************
************************************************************************************************
**  Summary stats [Table 2, Panel B]
************************************************************************************************
************************************************************************************************
sum dlnatr_tot_iv if year==2012 & tt1112_ncg==1 & bal_liab==1,d
sum datr_tot_iv if year==2012 & tt1112_ncg==1 & bal_liab==1,d
sum dliab_tot_iv_2012 if year==2012 & tt1112_ncg==1 & bal_liab==1,d
sum dpwliab_tot_iv_2012 if year==2012 & tt1112_ncg==1 & bal_liab==1,d
sum liab_tot  if year==2012 & tt1112_ncg==1 & bal_liab==1,d
reg liab_tot tt1112_ncg##i.post if year>=2011  & bal_liab==1 &  (tt1112_ncg==1 | tt1112_ncg==0)


************************************************************************************************
************************************************************************************************
**  Main - liab_tot [Figure 8, Table 4]
************************************************************************************************
************************************************************************************************

local bm1 = `baseyr'-1
local bp1 = `baseyr'+1
local r1 "i.netinc_`baseyr'cat##ib`baseyr'.year i.naics_`baseyr'##ib`baseyr'.year i.cz_`baseyr'##ib`baseyr'.year"

local tt "tt1112_ncg"
local t "dpwliab_tot_iv_2012"
forval y = `yrb' / `yre' {
	drop t`y'
	gen t`y' = 0
	replace t`y' = . if `t'==.
	replace t`y' = `t' if year==`y' & `tt'==1
}
* base 2011,2012
local events "t`yrb'-t2010 t`bp1'-t`yre' y`yrb'-y2010 y`bp1'-y`yre' `t'"

************
**  reduced form - all [Figure 8, Panel A]
************
foreach r in 1 {
	eststo r`r': quietly reghdfe dwages_ft `events'  [aw=n_ft_lower_2012] if (`tt'==1 | `tt'==0) & bal_liab==1, absorb(`r`r'' firm_tin) vce(cluster firm_tin)
}
esttab using "$OUTPUT/figure8_panelA.csv", replace se ar2 mtitles( ) keep(t20*) addnote("Each specification includes a different set of control variabels")
eststo clear

* dd version for table [Table 4, ITT]
drop dd
gen dd=`t'*post
local dr1 "i.netinc_`baseyr'cat##i.post i.naics_`baseyr'##i.post i.cz_`baseyr'##i.post"
local events "`t' post dd"

foreach r in 1 {
	eststo r`r': quietly reghdfe dwages_ft `events'  [aw=n_ft_lower_2012] if (`tt'==1 | `tt'==0) & bal_liab==1  & year>=2011 , absorb(`dr`r'' firm_tin) vce(cluster firm_tin)
}
esttab using "$OUTPUT/table4_all_itt.csv", replace se ar2 mtitles( ) keep(dd) addnote("Each specification includes a different set of control variabels")
eststo clear

************
**  reduced form - top only [Figure 8, Panel B]
************
local tt "tt1112_ncg"
local t "dpwliab_tot_iv_2012"
forval y = `yrb' / `yre' {
	drop t`y'
	gen t`y' = 0
	replace t`y' = . if `t'==.
	replace t`y' = `t' if year==`y' & `tt'==1
}

* base 2011,2012
local events "t`yrb'-t2010 t`bp1'-t`yre' y`yrb'-y2010 y`bp1'-y`yre' `t'"

foreach r in 1 {
	eststo r`r': quietly reghdfe dwages_ft `events'  [aw=n_ft_lower_2012] if `tt'==1 & bal_liab==1, absorb(`r`r'' firm_tin) vce(cluster firm_tin)
}
esttab using "$OUTPUT/figure8_panelB.csv", replace se ar2  mtitles( ) keep(t20*) addnote("Each specification includes a different set of control variabels")
eststo clear

************
**  first stage - yearly [Figure 8, Panel C]
************
local t "tt1112_ncg"
forval y = `yrb' / `yre' {
	drop t`y'
	gen t`y' = 0
	replace t`y' = . if `t'==.
	replace t`y' = `t' if year==`y'
}
* base 2011,2012
local events "t`yrb'-t2010 t`bp1'-t`yre' y`yrb'-y2010 y`bp1'-y`yre' `t'"

eststo fs: quietly reg liab_tot `events'  if (`t'==1 | `t'==0) & bal_liab==1 & year>=2011
esttab using "$OUTPUT/figure8_panelC.csv", replace se ar2 scalar(F) mtitles( ) keep(t2013 t2014 t2015 t2016) 
eststo clear

************
**  2sls - dd [Table 4]
************
local tt "tt1112_ncg"
drop dd
gen dd=`tt'*post
local dr1 "i.valaddpw_ft_2012cat##i.post i.netinc_`baseyr'cat##i.post i.naics_`baseyr'##i.post i.cz_`baseyr'##i.post"
local events "`tt' post dd"

* first stage
foreach r in 1 {
	quietly reg liab_tot `events' `dr`r'' if (`tt'==1 | `tt'==0) & year>=2011 & bal_liab==1
	predict yhatdd`r',xb
}
* F-Stat 
di e(F) 
* F-Stat = 235.9

* 2sls
foreach r in 1 {
	local t "yhatdd`r'"
	drop dd
	gen dd=`t'*post
	local events "`t' post dd"

	eststo w99_r`r': quietly reg dwwagebill_ft99 `events' `dr`r'' if (`tt'==1 | `tt'==0) & year>=2011 & bal_liab==1, vce(cluster firm_tin)
}
esttab using "$OUTPUT/table4_all_2sls.csv", replace se ar2 mtitles( ) keep(dd) 
eststo clear
drop yhat* 

**************
**  Elasticity - ATR [Table 4, elasticity]
**************
local trt "tt1112_ncg"
local t "dlnatr_tot_iv_2012"
forval y = `yrb' / `yre' {
	drop t`y'
	gen t`y' = 0
	replace t`y' = . if `t'==.
	replace t`y' = `t' if year==`y'
}
local events "t`yrb'-t2010 t`bp1'-t`yre' y`yrb'-y2010 y`bp1'-y`yre' `t'"

* loop over regressions
foreach r in 1 {
	eststo r`r': quietly reghdfe lnwages_ft `events'  [aw=n_ft_lower_2012] if (`trt'==1 | `trt'==0)  & bal_liab==1 & (year==2011 | year==2012 | year==2016), absorb(`r`r'' firm_tin) vce(cluster firm_tin)
}
esttab using "$OUTPUT/table4_all_elasticity.csv", replace se ar2 mtitles( ) keep(t2016) 
eststo clear



************************************************************************************************
************************************************************************************************
**  By Quartiles [Figure 7]
************************************************************************************************
************************************************************************************************
* Quartiles
foreach i in  dliab_tot_iv_2012 {
	gen `i'_q4=.
	foreach j in 2012 {
		capture drop xq
		xtile xq = `i' if year==`j' & tt1112_ncg==1 & dliab_tot_iv>0, nquantiles(4)
		replace `i'_q4=xq if year==`j' & tt1112_ncg==1 & dliab_tot_iv>0
	}
}
sum dliab_tot_iv_2012 if dliab_tot_iv_2012_q4==1
sum dliab_tot_iv_2012 if dliab_tot_iv_2012_q4==2
sum dliab_tot_iv_2012 if dliab_tot_iv_2012_q4==3
sum dliab_tot_iv_2012 if dliab_tot_iv_2012_q4==4

gen cat_tot4 = 0  if tt1112_ncg==1 & netinc_2012<0
replace cat_tot4 = 1 if tt1112_ncg==1 & dliab_tot_iv_2012>0
replace cat_tot4 = 2 if tt1112_ncg==1 & dliab_tot_iv_2012>5257.023
replace cat_tot4 = 3 if tt1112_ncg==1 & dliab_tot_iv_2012>15381.22
replace cat_tot4 = 4 if tt1112_ncg==1 & dliab_tot_iv_2012>41023
replace cat_tot4 = . if tt1112_ncg==1 &  dliab_tot_iv_2012==.
tab cat_tot4 if tt1112_ncg==1 & year==2012 

local t "tt1112_ncg"
forval y = `yrb' / `yre' {
	drop t`y'
	gen t`y' = 0
	replace t`y' = . if `t'==.
	replace t`y' = `t' if year==`y'
}
local events "t`yrb'-t2010 t2013-t`yre' y`yrb'-y2010 y2013-y`yre' `t'"
local r1 "i.netinc_`baseyr'cat##ib`baseyr'.year i.naics_`baseyr'##ib`baseyr'.year i.state_`baseyr'##ib`baseyr'.year"

local o "wages_ft"
foreach r in 1 {
foreach k in 0 1 2 3 4 {
	eststo q`k'_r`r': quietly reg ln`o' `events' `r`r''  [aw=n_ft_lower_2012] if (tt1112_ncg==0 | (tt1112_ncg==1  & cat_tot4==`k')) & (year==2011 | year==2012 | year==2016)
}
}
esttab using "$OUTPUT/figure7.csv", replace se ar2 mtitles( ) keep(t2016) 
eststo clear




************************************************************************************************
************************************************************************************************
**  Robust - liab all   [Table 5 calcs]
************************************************************************************************
************************************************************************************************
local bm1 = `baseyr'-1
local bp1 = `baseyr'+1

******
**  2sls 
******
local tt "tt1112_ncg"

drop dd
gen dd=`tt'*post
local dr1 "i.valaddpw_ft_2012cat##i.post i.netinc_`baseyr'cat##i.post i.naics_`baseyr'##i.post i.cz_`baseyr'##i.post"
local events "`tt' post dd"

* first stage
foreach r in 1 {
	quietly reg liab `events' `dr`r'' if (`tt'==1 | `tt'==0) & year>=2011 & bal_liab==1 
	predict yhatdd`r',xb
}

* 2sls
foreach r in 1 {
	local t "yhatdd`r'"
	drop dd
	gen dd=`t'*post
	local events "`t' post dd"

	eststo w99_r`r': quietly reg dwwagebill_ft99 `events' `dr`r'' if (`tt'==1 | `tt'==0) & year>=2011 & bal_liab==1 , vce(cluster firm_tin)
}
esttab using "$OUTPUT/table5_liab_all.csv", replace se ar2 mtitles( ) keep(dd) addnote("Each specification includes a different set of control variabels")
eststo clear

drop yhat* 






clear
exit
exit




